package com.sailfish.springbootdemo.dao.db2;

import com.sailfish.springbootdemo.common.DB2TableName;
import com.sailfish.springbootdemo.pojo.db2.PcbMaterialFactory;
import com.sailfish.springbootdemo.pojo.db2.PcbNewTechnologyFactory;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface PcbNewTechnologyFactoryDao {

    @Results(id = "pcbNewTechnologyFactoryResultMap", value = {
            @Result(column = "id", property = "id", jdbcType = JdbcType.INTEGER),
            @Result(column = "row_key", property = "rowKey", jdbcType = JdbcType.VARCHAR),
            @Result(column = "version", property = "version", jdbcType = JdbcType.VARCHAR),
            @Result(column = "new_technology_status", property = "newTechnologyStatus", jdbcType = JdbcType.VARCHAR),
            @Result(column = "responsibility_user_id", property = "responsibilityUserId", jdbcType = JdbcType.INTEGER),
            @Result(column = "responsibility_user_name", property = "responsibilityUserName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "approve_status", property = "approveStatus", jdbcType = JdbcType.VARCHAR),
            @Result(column = "approve_user_id", property = "approveUserId", jdbcType = JdbcType.INTEGER),
            @Result(column = "remark", property = "remark", jdbcType = JdbcType.VARCHAR),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "update_time", property = "updateTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER),
            @Result(column = "sort", property = "sort", jdbcType = JdbcType.INTEGER),
            @Result(column = "create_user", property = "createUser", jdbcType = JdbcType.VARCHAR),
            @Result(column = "update_user", property = "updateUser", jdbcType = JdbcType.VARCHAR),
    })

    /*
     * 查询单个
     */
    @Select("<script> " +
            "select * " +
            "from " + DB2TableName.PCB_NEW_TECHNOLOGY_FACTORY +
            "<where> " +
            "<if test='id != null'>" +
            " and id = #{id}" +
            "</if>" +
            "</where> " +
            " order by sort" +
            "</script>")
    PcbNewTechnologyFactory getById(@Param("id") Integer id);

    /*
     * 查询单个
     */
    @Select("<script> " +
            "select * " +
            "from " + DB2TableName.PCB_NEW_TECHNOLOGY_FACTORY +
            "<where> " +
            "<if test='rowKey != null'>" +
            " and row_key = #{rowKey}" +
            "</if>" +
            "</where> " +
            " order by sort" +
            "</script>")
    @ResultMap(value = "pcbNewTechnologyFactoryResultMap")
    PcbNewTechnologyFactory getByRowKey(@Param("rowKey") String rowKey);

    /*
     * 查询行
     */
    @Select("<script> " +
            "select * " +
            "from " + DB2TableName.PCB_NEW_TECHNOLOGY_FACTORY +
            "<where> " +
            "<if test='rowKey != null'>" +
            " and row_key = #{rowKey}" +
            "</if>" +
            "<if test='newTechnologyStatus != null'>" +
            " and new_technology_status = #{newTechnologyStatus}" +
            "</if>" +
            "<if test='approveStatus != null'>" +
            " and approve_status = #{approveStatus}" +
            "</if>" +
            "<if test='approveUserId != null'>" +
            " and approve_user_id = #{approveUserId}" +
            "</if>" +
            "</where> " +
            " order by sort" +
            "</script>")
    @ResultMap(value = "pcbNewTechnologyFactoryResultMap")
    List<PcbNewTechnologyFactory> getByLimit(@Param("rowKey") String rowKey,
                                             @Param("newTechnologyStatus") Integer newTechnologyStatus,
                                             @Param("approveStatus") String approveStatus,
                                             @Param("approveUserId") Integer approveUserId);

    @Select("<script> " +
            "select row_key " +
            "from " + DB2TableName.PCB_NEW_TECHNOLOGY_FACTORY +
            "<where> " +
            "<if test='newTechnologyStatus != null'>" +
            " and new_technology_status = #{newTechnologyStatus}" +
            "</if>" +
            "<if test='approveStatus != null'>" +
            " and approve_status = #{approveStatus}" +
            "</if>" +
            "<if test='approveUserId != null'>" +
            " and approve_user_id = #{approveUserId}" +
            "</if>" +
            "</where> " +
            " order by sort" +
            "</script>")
//    @ResultMap(value = "pcbNewTechnologyFactoryResultMap")
    List<String> getRowKeyByLimit(@Param("newTechnologyStatus") Integer newTechnologyStatus,
                                  @Param("approveStatus") String approveStatus,
                                  @Param("approveUserId") Integer approveUserId);


    /*
     *新增所有列
     */
    @Insert("insert into " + DB2TableName.PCB_NEW_TECHNOLOGY_FACTORY + " (row_key, version, new_technology_status, responsibility_user_id, responsibility_user_name, approve_status, approve_user_id, remark, create_time, create_user, update_time, update_user, status, sort) values " +
            "(#{rowKey}, #{version}, #{newTechnologyStatus}, #{responsibilityUserId}, #{responsibilityUserName}, #{approveStatus},#{approveUserId},#{remark}, #{createTime}, #{createUser}, #{updateTime}, #{updateUser}, #{status}, #{sort})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    Integer insert(PcbNewTechnologyFactory param);

    @Update("<script>" +
            "update " + DB2TableName.PCB_NEW_TECHNOLOGY_FACTORY +
            "<set>" +
            "<if test='newTechnologyStatus != null'>" +
            "new_technology_status = #{newTechnologyStatus}," +
            "</if>" +
            "<if test='responsibilityUserId != null'>" +
            "responsibility_user_id = #{responsibilityUserId}," +
            "</if>" +
            "<if test='responsibilityUserName != null'>" +
            "responsibility_user_name = #{responsibilityUserName}," +
            "</if>" +
            "<if test='approveStatus != null'>" +
            "approve_status = #{approveStatus}," +
            "</if>" +
            "<if test='approveUserId != null'>" +
            "approve_user_id = #{approveUserId}," +
            "</if>" +
            "<if test='remark != null'>" +
            "remark = #{remark}," +
            "</if>" +
            "</set>" +
            "where row_key = #{rowKey}" +
            "</script>")
    Integer updateStatus(PcbNewTechnologyFactory param);

    /*
     *通过rowKey删除
     */
    @Delete("delete from " + DB2TableName.PCB_NEW_TECHNOLOGY_FACTORY + " where row_key = #{rowKey} ")
    Integer deleteByRowKey(@Param("rowKey") String rowKey);
}
